<?php

namespace app\merchant\controller\store;

use app\merchant\controller\Base;
use app\merchant\model\store\Role as RoleModel;
use app\merchant\model\store\UserRole;
use app\merchant\model\store\Access as AccessModel;
use app\merchant\model\store\User;

/**
 * 商家用户角色控制器
 * Class StoreUser
 * @package app\merchant\controller\store;
 */
class Role extends Base
{


    /**
     * 获取权限树状结构
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function roleAccess()
    {
        $accessList = (new AccessModel)->getAccessTree();

        return $this->renderSuccess(compact('accessList'));

    }

    /**
     * 角色列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new RoleModel;
        $list = $model->getList();

        return $this->renderSuccess(compact('list'));

    }



    /**
     * 添加角色
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function add()
    {
        //获取数据
        $param = $this->postData();

        $model = new RoleModel;

        // 新增记录
        if ($model->add($param)) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError([],$model->getError() ?: '操作失败');
    }


    /**
     * 获取角色详情
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        //获取数据
        $param = $this->postData();
        if(!isset($param['role_id']) || isInt($param['role_id'])){
            return $this->renderError([],'参数错误');
        }
        //角色详情
        $datail = RoleModel::detail($param['role_id']);
        if(!$datail){
            return $this->renderError([],'角色不存在');
        }

        $datail['access'] = (new AccessModel)->getAccessTree($param['role_id']);

        return $this->renderSuccess($datail,'查询成功');

    }


    /**
     * 编辑角色
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        //获取数据
        $param = $this->postData();
        if(!isset($param['role_id']) || isInt($param['role_id'])){
            return $this->renderError([],'参数错误');
        }
        //角色详情
        $model = RoleModel::detail($param['role_id']);
        if(!$model){
            return $this->renderError([],'角色不存在');
        }

        // 更新记录
        if ($model->edit($param)) {
            return $this->renderSuccess([],'编辑成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除角色
     * @param $role_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        //获取数据
        $param = $this->postData();
        if(!isset($param['role_id']) || isInt($param['role_id'])){
            return $this->renderError([],'参数错误');
        }
        //角色详情
        $model = RoleModel::detail($param['role_id']);

        if(!$model){
            return $this->renderError([],'角色不存在');
        }

        if (!$model->remove()) {
            return $this->renderError([],$model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');

    }


    /**
     * 获取权限
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function getAccess()
    {
        $store_user_id = $this->store_user_id;
        //查询该管理员的角色
        $storeUser = (new User())->getLoginUser($store_user_id);
        if($storeUser['is_super']){
            $accessList = (new AccessModel)->getJsTree();
        }else{
            // 获取当前用户的角色集
            $roleIds = UserRole::getRoleIds($store_user_id);
            $accessList = (new AccessModel)->getJsTree($roleIds);
        }
        return $this->renderSuccess([
            'accessList' => $accessList,
        ]);
    }

}
